package com.ruoyi.project.business.workovertime.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.punch.entity.ScheduleClockPunch;
import com.ruoyi.project.business.punch.service.IScheduleClockPunchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.workovertime.mapper.WorkOvertimeMapper;
import com.ruoyi.project.business.workovertime.entity.WorkOvertime;
import com.ruoyi.project.business.workovertime.request.ListWorkOvertimeRequest;
import com.ruoyi.project.business.workovertime.request.AddWorkOvertimeRequest;
import com.ruoyi.project.business.workovertime.response.WorkOvertimeResponse;
import com.ruoyi.project.business.workovertime.service.IWorkOvertimeService;

/**
 * 加班Service业务层处理
 * 
 * @author ${author}
 * @date 2024-04-23
 */
@Service
public class WorkOvertimeServiceImpl implements IWorkOvertimeService {
    @Autowired
    private WorkOvertimeMapper workOvertimeMapper;
    @Autowired
    private IScheduleClockPunchService clockPunchService;

    /**
     * 查询加班
     * 
     * @return 加班
     */
    @Override
    public WorkOvertime selectWorkOvertimeById(Long id) {
        return workOvertimeMapper.selectWorkOvertimeById(id);
    }

    /**
     * 查询加班列表
     * 
     * @return 加班
     */
    @Override
    public List<WorkOvertimeResponse> selectWorkOvertimeList(ListWorkOvertimeRequest request, Long tenantId) {
        // TODO request to  param
        WorkOvertime param = BeanUtils.copy(request, WorkOvertime.class);
        param.setTenantId(tenantId);
        List<WorkOvertime> entitis = workOvertimeMapper.selectWorkOvertimeList(param);
        List<WorkOvertimeResponse> resList = BeanUtils.copyList(entitis, WorkOvertimeResponse.class);
        return resList;
    }

    /**
     * 新增加班
     * 
     * @return 结果
     */
    @Override
    public int insertWorkOvertime(AddWorkOvertimeRequest request, Long operatorId, Long tenantId) {
        // 查询 当天的打卡记录，（经审批后）记入加班
        LocalDate applyDate = request.getApplyDate();

        LocalDate yestoday = DateUtils.yestoday();
        if(applyDate.isAfter(yestoday)) {
            throw new CustomException("只能申请昨天之前的加班");
        }
        //
        ScheduleClockPunch scheduleClockPunch = clockPunchService.queryByDate(applyDate, operatorId, tenantId);
        if(scheduleClockPunch != null) {
            WorkOvertime entity = new WorkOvertime();
            entity.setUserId(operatorId);
            entity.setYear(applyDate.getYear());
            entity.setMonth(applyDate.getMonthValue());
            entity.setDayOfMonth(applyDate.getDayOfMonth());
            entity.setApplyTimeStart(LocalDateTime.of(applyDate, scheduleClockPunch.getClockTimeStart()));
            entity.setApplyTimeEnd(LocalDateTime.of(applyDate, scheduleClockPunch.getClockTimeEnd()));
            entity.setTenantId(tenantId);
            entity.setCreator(operatorId);
            entity.setDeleted(Boolean.FALSE);
            entity.setCreateTime(DateUtils.getNowDate());
            return workOvertimeMapper.insertWorkOvertime(entity);
        }
        throw new CustomException("当天没有打卡记录");
    }

    /**
     * 修改加班
     * 
     * @return 结果
     */
    @Override
    public int updateWorkOvertime(WorkOvertime entity, Long operatorId, Long tenantId) {
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setUpdater(operatorId);
        return workOvertimeMapper.updateWorkOvertime(entity);
    }

    /**
     * 批量删除加班
     * 
     * @return 结果
     */
    @Override
    public int deleteWorkOvertimeByIds(Long[] ids, Long operatorId, Long tenantId) {
        return workOvertimeMapper.deleteWorkOvertimeByIds(ids);
    }

    /**
     * 删除加班信息
     * 
     * @return 结果
     */
    @Override
    public int deleteWorkOvertimeById(Long id, Long operatorId, Long tenantId) {
        return workOvertimeMapper.deleteWorkOvertimeById(id);
    }
}
